#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from pymongo import MongoClient
import sys


def checkISSN():
    SERVER = 'localhost'
    PORT = 27017
    DB_NAME = "journal"
    COLLECTION_NAME = "journal"

    connection = MongoClient(SERVER, PORT)
    db = connection[DB_NAME]
    collection = db[COLLECTION_NAME]

    total_count = 0
    double_miss = 0
    only_pissn = 0
    only_pissn_miss = 0
    only_pissn_unique = 0
    only_pissn_multi = 0
    only_eissn = 0
    only_eissn_miss = 0
    only_eissn_unique = 0
    only_eissn_multi = 0
    both = 0
    both_miss = 0
    both_unique = 0
    both_multi = 0

    infile = open(sys.argv[1], "r")
    outfile = open(sys.argv[1] + ".noissn", "w")
    for line in infile:
        line = line.rstrip()
        mod_line = line[1:-1]
        splitline = mod_line.split("\",\"")
        pISSN = splitline[2].strip()
        eISSN = splitline[3].strip()
        total_count += 1
        if pISSN == "" and eISSN == "":
            double_miss += 1
            outfile.write(line + "\n")
            outfile.flush()
        elif pISSN != "" and eISSN == "":
            only_pissn += 1
            results = collection.find({"pISSN": pISSN}).count()
            if results == 0:
                only_pissn_miss += 1
            elif results == 1:
                only_pissn_unique += 1
            elif results > 1:
                only_pissn_multi += 1
        elif pISSN == "" and eISSN != "":
            only_eissn += 1
            results = collection.find({"eISSN": eISSN}).count()
            if results == 0:
                only_eissn_miss += 1
            elif results == 1:
                only_eissn_unique += 1
            elif results > 1:
                only_eissn_multi += 1
        elif pISSN != "" and eISSN != "":
            both += 1
            results = collection.find({"pISSN": pISSN, "eISSN": eISSN}).count()
            if results == 0:
                both_miss += 1
            elif results == 1:
                both_unique += 1
            elif results > 1:
                both_multi += 1
    infile.close()
    outfile.close()
    connection.close()
    print "TOTAL: " + str(total_count)
    print "DOUBLE MISS: " + str(double_miss)
    print "ONLY pISSN: " + str(only_pissn)
    print "ONLY pISSN MISS: " + str(only_pissn_miss)
    print "ONLY pISSN UNIQUE: " + str(only_pissn_unique)
    print "ONLY pISSN MULTI: " + str(only_pissn_multi)
    print "ONLY eISSN: " + str(only_eissn)
    print "ONLY eISSN MISS: " + str(only_eissn_miss)
    print "ONLY eISSN UNIQUE: " + str(only_eissn_unique)
    print "ONLY eISSN MULTI: " + str(only_eissn_multi)
    print "BOTH: " + str(both)
    print "BOTH MISS: " + str(both_miss)
    print "BOTH UNIQUE: " + str(both_unique)
    print "BOTH MULTI: " + str(both_multi)


def checkMedAbbr():
    SERVER = 'localhost'
    PORT = 27017
    DB_NAME = "journal"
    COLLECTION_NAME = "journal"

    connection = MongoClient(SERVER, PORT)
    db = connection[DB_NAME]
    collection = db[COLLECTION_NAME]

    total_count = 0
    abbr_miss = 0
    abbr_unique = 0
    abbr_multi = 0
    infile = open(sys.argv[1], "r")
    for line in infile:
        total_count += 1
        line = line.rstrip()
        mod_line = line[1:-1]
        splitline = mod_line.split("\",\"")
        MedAbbr = splitline[1].strip()
        if MedAbbr != "":
            results = collection.find({"MedAbbr": MedAbbr}).count()
            if results == 0:
                abbr_miss += 1
                print "MEDABBR MISS====" + line
            elif results == 1:
                abbr_unique += 1
            elif results > 1:
                abbr_multi += 1
                print "MEDABBR MULTI====" + line
    infile.close()
    print "TOTAL: " + str(total_count)
    print "MEDABBR MISS: " + str(abbr_miss)
    print "MEDABBR UNIQUE: " + str(abbr_unique)
    print "MEDABBR MULTI: " + str(abbr_multi)


def checkFullTitle():
    SERVER = 'localhost'
    PORT = 27017
    DB_NAME = "journal"
    COLLECTION_NAME = "journal"

    connection = MongoClient(SERVER, PORT)
    db = connection[DB_NAME]
    collection = db[COLLECTION_NAME]

    total_count = 0
    title_miss = 0
    title_unique = 0
    title_multi = 0

    infile = open(sys.argv[1], "r")
    outfile = open(sys.argv[1] + ".noissn", "w")
    for line in infile:
        line = line.rstrip()
        mod_line = line[1:-1]
        splitline = mod_line.split("\",\"")
        FullTitle = splitline[0].strip()
        if FullTitle != "":
            splittitle=FullTitle.split(" ")
            regex="^"
            for item in splittitle:
                regex+=item.strip()+"[,: ]*"
            regex+="$"
            # results = collection.find({"FullTitle": FullTitle}).count()
            results = collection.find({"FullTitle": {"$regex":regex,"$options":"$i"}}).count()
            if results == 0:
                title_miss += 1
                # print "FULLTITLE MISS====" + line
            elif results == 1:
                title_unique += 1
            elif results > 1:
                title_multi += 1
                print "FULLTITLE MULTI====" + line
    infile.close()
    print "TOTAL: " + str(total_count)
    print "MEDABBR MISS: " + str(title_miss)
    print "MEDABBR UNIQUE: " + str(title_unique)
    print "MEDABBR MULTI: " + str(title_multi)


if __name__ == "__main__":
    # checkISSN()
    # checkMedAbbr()
    checkFullTitle()
